﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Thesis_v01
{
   public partial class EditPassword : Form
   {
      private CourseDetail currentDetails;

      public EditPassword(CourseDetail details)
      {
         InitializeComponent();

         currentDetails = details;
      }

      private void EditPassword_Load(object sender, EventArgs e)
      {
         passwordTextBox.Text = currentDetails.Password;
         restoreButton.Enabled = false;
         changeButton.Enabled = false;
         passwordLabel.Text = "Current password:";
      }

      private void passwordTextBox_TextChanged(object sender, EventArgs e)
      {
         restoreButton.Enabled = true;
         changeButton.Enabled = true;
         passwordLabel.Text = "Password changed to:";
      }

      private void cancelButton_Click(object sender, EventArgs e)
      {
         //MainApp main = (MainApp)this.MdiParent;
         //main.ToggleCourseListEnable();
         this.Close();
      }

      private void restoreButton_Click(object sender, EventArgs e)
      {
         restoreButton.Enabled = false;
         changeButton.Enabled = false;
         passwordLabel.Text = "Current password:";
         passwordTextBox.Text = currentDetails.Password;
      }

      private void changeButton_Click(object sender, EventArgs e)
      {
         currentDetails.Password = passwordTextBox.Text;

         DBConnection dbConn = new DBConnection();
         dbConn.UpdateCoursePassword(currentDetails.CourseID, currentDetails.Password);

         MainApp main = (MainApp)this.MdiParent;
         main.UpdateCourseDetail(currentDetails);

         //main.ToggleCourseListEnable();
         this.Close();
      }

      private void EditPassword_FormClosing(object sender, FormClosingEventArgs e)
      {
         MainApp main = (MainApp)this.MdiParent;
         main.ToggleCourseListEnable();
         //this.Close();
      }
   }
}
